package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.constants.CommonConstants;
import net.zigin.nhi.could.waste.dao.*;
import net.zigin.nhi.could.waste.dto.WasteBoxDto;
import net.zigin.nhi.could.waste.exception.EpiCloudException;
import net.zigin.nhi.could.waste.model.HospitalBase;
import net.zigin.nhi.could.waste.util.EncryptDataUtil;
import net.zigin.nhi.could.waste.vo.TokenInfoBo;
import net.zigin.nhi.could.waste.vo.WasteBoxVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.model.WasteBox;
import net.zigin.nhi.could.waste.service.WasteBoxService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-13 21:14:43                                               
##################################################################*/


/**
 * WasteBox服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: WasteBoxServiceImpl
 * @date 2021-9-13 21:14:43
 */
@Service
public class WasteBoxServiceImpl extends AbstractServiceImpl<WasteBox, String> implements WasteBoxService {
    @Autowired
    private WasteBoxMapper wasteBoxMapper;
    @Autowired
    private SysUserHospitalMapper sysUserHospitalMapper;
    @Autowired
    private HospitalBaseMapper hospitalBaseMapper;
    @Autowired
    private WasteBaseMapper wasteBaseMapper;

    @Override
    public BaseMapper<WasteBox, String> getMapper() {
        return wasteBoxMapper;
    }

    @Override
    public String saveOrUpdate(WasteBoxDto wasteBoxDto) {
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        WasteBox wasteBox = ReflectUtil.copyProperties(wasteBoxDto, WasteBox.class);
        if (StringUtil.isNullOrEmpty(wasteBox.getId())) {
            String hospitalBaseId = sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId());
            HospitalBase hospitalBase = hospitalBaseMapper.selectByPrimaryKey(hospitalBaseId);
            wasteBox.setId(StringUtil.get32UUID());
            wasteBox.setCode(generateXZCode(hospitalBase));
            wasteBox.setHospitalBaseId(hospitalBaseId);
            wasteBox.setCreateTime(new Date());
            wasteBox.setModifyTime(new Date());
            wasteBox.setCreateUser(tokenInfo.getLoginName());
            wasteBoxMapper.insert(wasteBox);
        } else {
            wasteBox.setModifyTime(new Date());
            wasteBox.setModifyUser(tokenInfo.getLoginName());
            wasteBoxMapper.updateByPrimaryKeySelective(wasteBox);
        }
        return wasteBox.getId();
    }

    private String generateXZCode(HospitalBase hospitalBase) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("XZ").append(hospitalBase.getNum()).append(String.format("%04d", hospitalBase.getXzNum()));
        HospitalBase temp = new HospitalBase();
        temp.setId(hospitalBase.getId());
        temp.setXzNum(hospitalBase.getXzNum() + 1);
        hospitalBaseMapper.updateByPrimaryKeySelective(temp);
        return stringBuffer.toString();
    }

    @Override
    public List<WasteBoxVo> getList(WasteBoxDto wasteBoxDto) {
        Map<String, Object> paramMap = getWasteBoxParam(wasteBoxDto);
        List<WasteBoxVo> wasteBoxes = wasteBoxMapper.getPageListByCondition(paramMap);
        if (wasteBoxes.isEmpty()) {
            return new ArrayList<>();
        }
        wasteBoxes.forEach(wasteBoxVo -> {
            wasteBoxVo.setCollectWeight(wasteBoxVo.getCollectWeight() + wasteBoxVo.getWeight());
            wasteBoxVo.setContent(this.generateQrCode(wasteBoxVo.getId()));
        });
        return wasteBoxes;
    }


    @Override
    public Integer getCount(WasteBoxDto wasteBoxDto) {
        Map<String, Object> paramMap = getWasteBoxParam(wasteBoxDto);
        return wasteBoxMapper.getPageCountByCondition(paramMap);
    }

    private Map<String, Object> getWasteBoxParam(WasteBoxDto wasteBoxDto) {
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        String hospitalBaseId = sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId());
        return ParamsMapUtil.getInstance()
                .addParam("orderBy", wasteBoxDto.getOrderBy())
                .addParam("hospitalBaseId", hospitalBaseId)
                .addParam(wasteBoxDto.getOperation(), "1")
                .addParam("wasteBoxIds", wasteBoxDto.getWasteBoxIds())
                .addParam("pageSize", wasteBoxDto.getPageSize())
                .addParam("pageIndex", wasteBoxDto.getPageIndex());
    }

    @Override
    public String generateQrCode(String id) {
        WasteBox wasteBox = wasteBoxMapper.selectByPrimaryKey(id);
        if (wasteBox == null) {
            throw new EpiCloudException("医废箱不存在");
        }
        return EncryptDataUtil.encryptData(CommonConstants.QRCODE_WASTE_BOX, wasteBox.getId());
    }

    @Override
    public List<WasteBoxVo> getPendingInStorageBoxList() {
        //
        return null;
    }

    @Override
    public List<WasteBoxVo> getPendingOutStorageBoxList() {
        return null;
    }

    @Override
    public WasteBoxVo getAllInfoById(String id) {
        WasteBoxVo wasteBoxVo = wasteBoxMapper.getAllInfoById(id);
        wasteBoxVo.setCollectWeight(wasteBoxVo.getCollectWeight() + wasteBoxVo.getWeight());
        return wasteBoxVo;
    }
}